<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 100px;
        height: 100px;
        background: red;
        position: absolute;
        left: 0px;
        top: 0px;
      }
    </style>
  </head>
  <body>
    <div class="box"></div>
  </body>
</html>
<script>
  //让方块向右运动到300px,再向下运动300px停止
  function move(boxEle, dir, dis, cb) {
    var timer = setInterval(function () {
      //获取非行间样式
      var num = parseInt(getComputedStyle(boxEle)[dir]);
      var speed = 5;
      boxEle.style[dir] = num + speed + "px";
      if (num + speed == dis) {
        clearInterval(timer);
        cb && cb();
      }
    }, 10);
  }
  var boxEle = document.querySelector(".box");
  move(boxEle, "left", 300, function () {
    move(boxEle, "top", 300, function () {});
  });
</script>
